package com.autoEcole.service;


import java.util.List;
import java.util.Map;

import com.autoEcole.dao.CoursDAO;
import com.autoEcole.exception.ApplicationException;
import com.autoEcole.exception.DAOException;
import com.autoEcole.model.Cours;
import com.autoEcole.service.CoursService;

public class CoursService {
	
	private CoursDAO coursDAO;
	/**
	 * CoursService
	 */
	private static CoursService coursService;
	
	/**
	 * Constructor
	 */
	private CoursService() {
		super();
	}

	/**
	 * Returns the CoursService instance (Design Pattern : Singleton)
	 * 
	 * @return CoursService
	 */
	public static synchronized CoursService getInstance() {
		if (coursService == null)
			coursService = new CoursService();
		return coursService;
	}

	public CoursDAO getCoursDAO() {
		return coursDAO;
	}

	public void setCoursDAO(CoursDAO coursDAO) {
		this.coursDAO = coursDAO;
	}

	public int create(Cours cours) throws ApplicationException {
		try {
			return coursDAO.create(cours);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * 
	 */
	public void update(Cours cours) throws ApplicationException {
		try {
			coursDAO.update(cours);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * retourne l'element si il existe ou null
	 */
	public Cours findById(int id) throws ApplicationException {
		try {
			return coursDAO.findById(id);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * 
	 */
	public List<Cours> findAll() throws ApplicationException {
		try {
			return coursDAO.findAll();
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}		
	}
	
	/**
	 * 
	 */
	public Map<Integer , Cours> findAllMap() throws ApplicationException {
		try {
			return coursDAO.findAllMap();
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}
	
	/**
	 * 
	 */	
	public void delete(int coursId) throws ApplicationException {
		try {
			coursDAO.delete(coursId);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}		
	}	
}
